Search and replace with line breaks
Search and replace with line breaks
am 14.11.2007 23:33:46 von laredotornado
Hi,
I'm running Fedora Core 5 Linux. I know how to do a search and
replace when the string is on one line, but what happens if it spans a
line break? I want to take this pattern of text
class="style38">
alt="wk30" width="50" height="20" border="0" /> |
|
and replace it with
class="style38">
alt="wk29" width="50" height="20" border="0" /> |
|
How could I do this across a bunch of files in the same directory?
Thanks, - Dave
Re: Search and replace with line breaks
am 15.11.2007 07:02:25 von Bill Marcum
On 2007-11-14, laredotornado@zipmail.com wrote:
> Hi,
>
> I'm running Fedora Core 5 Linux. I know how to do a search and
> replace when the string is on one line, but what happens if it spans a
> line break? I want to take this pattern of text
>
>
> class="style38">
> alt="wk30" width="50" height="20" border="0" /> |
> |
>
> and replace it with
>
>
> class="style38">
> alt="wk29" width="50" height="20" border="0" /> |
> |
>
> How could I do this across a bunch of files in the same directory?
>
> Thanks, - Dave
>
You could use awk with something other than \n as the record separator (RS).
Re: Search and replace with line breaks
am 15.11.2007 17:46:56 von laredotornado
On Nov 15, 12:02 am, Bill Marcum wrote:
> On 2007-11-14, laredotorn...@zipmail.com wrote:
>
>
>
> > Hi,
>
> > I'm running Fedora Core 5 Linux. I know how to do a search and
> > replace when the string is on one line, but what happens if it spans a
> > line break? I want to take this pattern of text
>
> >
> > class="style38">
> > alt="wk30" width="50" height="20" border="0" /> |
> > |
>
> > and replace it with
>
> >
> > class="style38">
> > alt="wk29" width="50" height="20" border="0" /> |
> > |
>
> > How could I do this across a bunch of files in the same directory?
>
> > Thanks, - Dave
>
> You could use awk with something other than \n as the record separator (RS).- Hide quoted text -
>
> - Show quoted text -
Hi there, 'awk' is giving me errors when I try and switch the record
separator. I'm supposed to use "-RC '\r'" right? Anyway, here's
what's happening ...
[laredo@remandev]~/w/dh/v2/show/archive2% awk -RC '\r' '{gsub(" |
align=\"left\" valign=\"top\">
\"style38\">
alt=\"wk30\" width=\"50\" height=\"20\" border=\"0\" />
\n
href=\"/index.html\" target=\"_self\">
nowplaying.jpg\" alt=\"np\" width=\"95\" height=\"40\" border=\"0\" /
> | ", "
\"center\" class=\"style38\">
images/wk29.jpg\" alt=\"wk29\" width=\"50\" height=\"20\" border=
\"0\" /> | \n |
\">", $0); print > FILENAME}' *
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options:
-f progfile --file=progfile
-F fs --field-separator=fs
-v var=val --assign=var=val
-m[fr] val
-W compat --compat
-W copyleft --copyleft
-W copyright --copyright
-W dump-variables[=file] --dump-variables[=file]
-W exec=file --exec=file
-W gen-po --gen-po
-W help --help
-W lint[=fatal] --lint[=fatal]
-W lint-old --lint-old
-W non-decimal-data --non-decimal-data
-W profile[=file] --profile[=file]
-W posix --posix
-W re-interval --re-interval
-W source=program-text --source=program-text
-W traditional --traditional
-W usage --usage
-W version --version
To report bugs, see node `Bugs' in `gawk.info', which is
section `Reporting Problems and Bugs' in the printed version.
gawk is a pattern scanning and processing language.
By default it reads standard input and writes standard output.
Examples:
gawk '{ sum += $1 }; END { print sum }' file
gawk -F: '{ print $1 }' /etc/passwd
Thanks, - Dave
Re: Search and replace with line breaks
am 15.11.2007 22:39:41 von Ed Morton
On 11/15/2007 10:46 AM, laredotornado@zipmail.com wrote:
> On Nov 15, 12:02 am, Bill Marcum wrote:
>
>>On 2007-11-14, laredotorn...@zipmail.com wrote:
>>
>>
>>
>>
>>>Hi,
>>
>>>I'm running Fedora Core 5 Linux. I know how to do a search and
>>>replace when the string is on one line, but what happens if it spans a
>>>line break? I want to take this pattern of text
>>
>>>
>>>class="style38">
>>>alt="wk30" width="50" height="20" border="0" /> |
>>> |
>>
>>>and replace it with
>>
>>>
>>>class="style38">
>>>alt="wk29" width="50" height="20" border="0" /> |
>>> |
>>
>>>How could I do this across a bunch of files in the same directory?
>>
>>>Thanks, - Dave
>>
>>You could use awk with something other than \n as the record separator (RS).- Hide quoted text -
>>
>>- Show quoted text -
>
>
> Hi there, 'awk' is giving me errors when I try and switch the record
> separator. I'm supposed to use "-RC '\r'" right?
No, "-v RS='\r'" seems to be what you're trying to do. You'd know best if '\r'
makes sense as an RS for your input. There is a man page and Bill specifically
told you to set RS...
> Anyway, here's what's happening ...
>
> [laredo@remandev]~/w/dh/v2/show/archive2% awk -RC '\r' '{gsub(" |
> align=\"left\" valign=\"top\">
> \"style38\">
> alt=\"wk30\" width=\"50\" height=\"20\" border=\"0\" />
> \n
> href=\"/index.html\" target=\"_self\">
> nowplaying.jpg\" alt=\"np\" width=\"95\" height=\"40\" border=\"0\" /
>
>> | ", "
>
> \"center\" class=\"style38\">
> images/wk29.jpg\" alt=\"wk29\" width=\"50\" height=\"20\" border=
> \"0\" /> | \n |
> \">", $0); print > FILENAME}' *
You REALLY don't want to try to print to the same file you're reading (FILENAME)
while you're reading it!
Ed.